package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.annotation.UserParams;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Created by 甘静斌 on 2020-08-11 21:19
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object query(TravelQuery qo){

        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

    /**
     * 旅游日记--写游记发表操作
     * @param travel
     * @return
     */
    @LoginAnnotation
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){

        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        travel.setUserId(user.getId());
        travelService.saveOrUpdate(travel);

        return JsonResult.success(travel.getId());
    }


    /**
     * 用户注入对象 ,演示是否正确
     * @param userInfo
     * @return
     */
    @GetMapping("/info")
    public Object info(@UserParams UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    /**
     * 游记的明细
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(String id, @UserParams UserInfo userInfo){
        //根据id获取travel对象
        if (StringUtils.hasLength(id)){

            Travel travel = travelService.get(id);
            //页面报错,headImgUrl 报错 , 需要设置author 获取到headImgUrl
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            //获取当前目的地id,得到目的地, 然后设置进去
            travel.setDest(destinationService.get(travel.getDestId()));
            return JsonResult.success(travel);
        }
            return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments(String travelId){

        List<TravelComment> list =  travelCommentService.queryByTravelId(travelId);
        return JsonResult.success(list);
    }

    /**
     * 游记评论和关联评论
     * @param travelComment
     * @param userInfo
     * @return
     */
    @PostMapping("/commentAdd")
    @LoginAnnotation
    public Object commentAdd(TravelComment travelComment,@UserParams UserInfo userInfo){

        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);

        return JsonResult.success();

    }
}
